$(document).ready(
				function() {
					
					var totalCount = $("#totalCount").val();
					var pSize=$("#pageSize").val();;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					

					$("#userDetails").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#userInfor").attr("method","post");
										
										var userId = $("#userId").val();
									
											if (userId =="") {
												
												 $("#userInfor").attr("action","save.do");
											} else {
												
												 $("#userInfor").attr("action","update.do");
											}
											$("#userInfor").submit();
										
									});

					$('#pagination2').jqPaginator(
									{
										totalCounts:totalCount,
										pageSize:pSize,
										visiblePages : pageCount,
										currentPage : 1,
										
										/* first: '<li class="first"><a href="javascript:void(0);">第一页</a></li>',*/
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										   /* last: '<li class="last"><a href="javascript:void(0);">Last</a></li>',*/
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
										/*	alert("pageIndex="+num);
										alert("type="+type);*/
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {

															
															$("#userList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var userInfor = results[i];

																var rowContent = "<tr>"
																		+ "<td style='text-align: center;'><input name='userId' class='checkbox' type='checkbox' value='"
																		+ userInfor.id
																		+ "'/></td>"
																		+ "<td style='text-align: center;'>"
																		+ userInfor.id
																		+ "</td>"
																		+ "<td style='text-align: center;'>"
																		+ userInfor.loginName
																		+ "</td>"
																		+ "<td style='text-align: center;'>"
																		+ userInfor.email
																		+ "</td>"
																		+ "<td style='text-align: center;'>";
																if (userInfor.status == 0) {
																	rowContent = rowContent
																			+ "正常";
																} else {
																	rowContent = rowContent
																			+ "<font color='red'>已锁定</font>";
																}

																rowContent = rowContent
																		+ "</td>";
																rowContent = rowContent
																		+ "<td style='text-align: center;'><button name='editBtn' objId='"
																		+ userInfor.id
																		+ "' class='btn btn-sm-block'>修改</button></td>";
																rowContent = rowContent
																+ "<td style='text-align: center;'>";
																if (userInfor.status == 0) {
																	rowContent = rowContent+
																	"<button name='lockBtn' objId='"
																	+ userInfor.id
																	+ "' class='btn btn-sm-block'>锁定用户</button>" ;
																} else {
																	rowContent = rowContent+
																	"<button name='unlockBtn' objId='"
																	+ userInfor.id
																	+ "' class='btn btn-sm-block'>解锁用户</button>" ;
																}
																		
																rowContent = rowContent+
																		"</td>";
																rowContent = rowContent	+ "</tr>";
																$("#userList")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#userList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='userId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#userList").delegate("button[name='editBtn']", 'click', function() {
						var userId = $(this).attr("objId");
						
						var editUrl = "show.do?userId=" + userId;
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : userId
							},
							cache : false,

							success : function(data) {
								
								loadUser(data);
							},
							error : function(html) {
								// alert(html);
								alert("Load Data failly");
								refresh();
							}
						});

					});
					
					$("#userList").delegate("button[name='lockBtn']", 'click', function() {
						var userId = $(this).attr("objId");
						
						var editUrl = "lockUser.do?userId=" + userId;
						if(confirm("是否要锁定用户?")){
						$.ajax({

							url : editUrl,
							dataType : 'text',
							data : {
								id : userId
							},
							cache : false,

							success : function(data) {
								
								if(data=="success"){
									alert("锁定用户成功"+$(this).text());
									refresh();
								}else{
									alert("锁定用户失败");
									refresh();
								}
								
							},
							error : function(html) {
								// alert(html);
								alert("锁定用户失败");
								refresh();
							}
						});
						}

					});
					
					$("#userList").delegate("button[name='unlockBtn']", 'click', function() {
						var userId = $(this).attr("objId");
						
						var editUrl = "unlockUser.do?userId=" + userId;
						if(confirm("是否要解锁用户?")){
						$.ajax({

							url : editUrl,
							dataType : 'text',
							data : {
								id : userId
							},
							cache : false,

							success : function(data) {
								
								if(data=="success"){
									alert("解锁用户成功"+$(this).text());
									refresh();
								}else{
									alert("解锁用户失败");
									refresh();
								}
								
							},
							error : function(html) {
								// alert(html);
								alert("解锁用户失败");
								refresh();
							}
						});
						}

					});			

					$("#tools")
							.delegate(
									'#deleteBtn',
									'click',
									function() {
										delChk = $('input[name="userId"]:checked');

										if (delChk.length > 0) {
											if (confirm("Are you sure delete the record?")) {

												var ids = "";
												/*var delUrl = "delete.do?ids=";*/
												var delUrl = "delete.do";
                                                var idArr=new Array();
                                               
                                                
                                                for (var i = 0; i < delChk.length; i++) {
                                                	idArr.push(delChk.get(i).value);
                                           
											}
												
												$.ajax({
															url : delUrl,
															type: "POST",
															data : {
																ids : idArr.toString()
															},
															cache : false,

															success : function(
																	html) {
																alert("Delete successfully");
																refresh();
															},
															error : function(
																	html) {
																alert("Delete failly");
																refresh();
															}
														});
											} else {
												return;
											}
										} else {
											alert("Please select the record to delete");
											return;
										}
									});

					$("#tools").delegate('#addUser', 'click', function() {
						
						
						$("#userDetails input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);

						});

						$("#userDetails textarea").each(function() {
							$(this).prop("value", null);

						});
						
						$("#userDetails select").each(function() {
							$(this).prop("value", 0);

						});
						$("#userDetails").modal('show');

					});
					
					
					

				});





function loadUser(user) {
   
	$("#userId").val(user.id);
	$("#loginName").val(user.loginName);
	$("#email").val(user.email);
	$("#password").val(user.password);
	$("#status").val(user.status);
	$("#note").val(user.note);
	$("#userDetails").modal('show');
}

function vaildate(form) {
	return true;
}

function refresh() {
	window.location.reload();
}
